package cn.erp.app.util;

import java.io.IOException;
import java.io.PrintWriter;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;

public class EuiRender extends Render {
	private static final String CONTENT_TYPE = "text/json;charset="
			+ getEncoding();

	private Object data;
	
	public static EuiRender page(Page<?> _page) {
		EuiRender eui = new EuiRender();
		Record p = new Record();
		p.set("total", _page.getTotalRow());
		p.set("rows", _page.getList());
		eui.data = p;
		return eui;
	} 

	@Override
	public void render() {
		// TODO Auto-generated method stub
		PrintWriter writer = null;

		String str = JsonKit.toJson(data);

		try {
			response.setHeader("Pragma", "no-cache"); // HTTP/1.0 caches might
														// not implement
														// Cache-Control and
														// might
														// only implement
														// Pragma:
														// no-cache
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.setContentType(CONTENT_TYPE);
			writer = response.getWriter();
			writer.write(str);
			writer.flush();
		} catch (IOException e) {
			throw new RenderException(e);
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}
}
